"""
自定义序列化器（把模型对象序列化为字典）
"""
from rest_framework import serializers
from polls.models import Record, Car


class CarSerializer(serializers.ModelSerializer):
    """车序列化器"""

    class Meta:
        model = Car
        fields = ('carno', 'owner')


class RecordSerializer(serializers.ModelSerializer):
    """违章记录序列化器"""

    # 对car属性进行序列化，car是一个外键列
    car = serializers.SerializerMethodField()

    @staticmethod
    def get_car(record):
        # .data属性拿到字典
        # return {'carno': record.car.no, 'owner': record.car.owner} ----> 等同如下语句
        return CarSerializer(record.car).data

    class Meta:
        model = Record
        # 指定需要序列化的所有字段
        # fields = '__all__'
        # 通过元组指定序列化字段
        # fields = ('no', 'reason', 'punish', 'makedate', 'dealt', 'car')
        exclude = ('is_deleted', 'deleted_time', 'updated_time')
